﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.ApprovalRecordCommand;
using T.Materials.Read.API.ApplicationRead.Command.ExamineAndApproveCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStatisticsCommand;
using T.Materials.Read.API.ApplicationRead.CommandHandler.ApprovalRecordCommandHandler;
using T.Materials.Read.API.ApplicationRead.CommandHandler.ExamineAndApproveCommandHadnler;

namespace T.Materials.Read.API.Controllers
{
    /// <summary>
    /// API控制器--审批
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExamineAndApproveController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ExamineAndApproveController(IMediator mediator)
        {
            this.mediator = mediator;
        }


        /// <summary>
        /// 物资采购审批列表
        /// </summary>
        /// <param name="command">查询审批命令</param>
        /// <returns>返回审批物资列表</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialStoreDto<MaterialStoreDto>>> GetExamineAndApproveList([FromQuery] ExamineAndApproveMaterialStatisticsCommand command)
        {
            ApiResult<ListMaterialStoreDto<MaterialStoreDto>> result = new ApiResult<ListMaterialStoreDto<MaterialStoreDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询物资采购审批列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示物资采购审批记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MApproval>>> GetApprovals([FromQuery] Approowlist command)
        {
            ApiResult<List<MApproval>> result = new ApiResult<List<MApproval>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示物资领用审批记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MApproval>>> GetMaterialRequisitionApprovalRecordList([FromQuery] MaterialRequisitionApprovalRecordCommand command)
        {
            ApiResult<List<MApproval>> result = new ApiResult<List<MApproval>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 物资领用审批列表
        /// </summary>
        /// <param name="command">查询审批命令</param>
        /// <returns>返回审批物资列表</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialRequisitionDto<MaterialRequisition>>> GetMaterialRequisitionApproveList([FromQuery] MaterialRequisitionApproveCommand command)
        {
            ApiResult<ListMaterialRequisitionDto<MaterialRequisition>> result = new ApiResult<ListMaterialRequisitionDto<MaterialRequisition>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询物资采购审批列表失败";
                throw;
            }
            return result;
        }

    }
}
